package io.gitee.kotle.base.library

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.lifecycle.LiveData
import io.gitee.kotle.android.mvvm.MvvmActivity
import io.gitee.kotle.android.mvvm.mvvminterface.MvvmViewType
import io.gitee.kotle.android.utils.*
import io.gitee.kotle.base.OnStatusDataAttachViewListener
import io.gitee.kotle.base.observerStatusDataAttachView

/**
 *
 * 描述：
 *
 * 创建人：jiale.wei
 * 创建时间：2021/11/23 11:14 上午
 *
 */
abstract class BaseActivity : MvvmActivity(), IUiViewHelper by UiViewHelperImpl() {

    override fun beforeMvvmCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): ViewGroup? {
        return createBaseUiView(
            inflater,
            container,
            savedInstanceState,
            isNeedToolBar(),
            isNeedBaseUiView()
        )
    }

    override fun afterMvvmCreateView(
        inflater: LayoutInflater,
        beforeView: ViewGroup?,
        createView: MvvmViewType?,
        savedInstanceState: Bundle?
    ): MvvmViewType? {
        return handleBaseUiView(inflater, beforeView, createView, savedInstanceState)
    }


    /**
     * 绑定data
     */
    inline fun <T> LiveData<StatusData<T>>.observerAttachView(
        crossinline doSuccess: Function1<T, Unit>,
    ) = observerStatusDataAttachView(
        this@BaseActivity,
        this@BaseActivity,
        doSuccess
    )

    fun <T> LiveData<StatusData<T>>.observerAttachView(
        listener: OnStatusDataAttachViewListener<T>,
    ) = observerStatusDataAttachView(
        this@BaseActivity,
        this@BaseActivity,
        listener
    )


    /**
     * 绑定data
     */
    fun <T> LiveData<StatusData<T>>.observerStatus(block: StatusDataObserver<T>.() -> Unit) =
        observeStatusData(this@BaseActivity, block)

    /**
     * 绑定data
     */
    inline fun <T> LiveData<StatusData<T>>.doSuccess(crossinline block: Function1<T, Unit>) =
        doOnSuccess(this@BaseActivity, block)
}